package ru.aptu.gsp_banking.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.aptu.gsp_banking.domain.Card;
import ru.aptu.gsp_banking.service.AccountService;
import ru.aptu.gsp_banking.service.CardService;
import ru.aptu.gsp_banking.snmp.Sender;

import java.security.Principal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 11/18/11
 * Time: 1:34 AM
 */

@Controller
public class CardController {

    @Autowired
    private CardService cardService;

    @Autowired
    private AccountService accountService;

    private Integer accountId;

    @RequestMapping("/cards/{accountId}")
    public String listCards(@PathVariable("accountId") Integer accountId, Map<String, Object> map, Principal principal) {
        if (principal == null) {
            return "redirect:../logout";
        }

        this.accountId = accountId;

        if (accountService.checkAccount(accountId, principal.getName())) {
            map.put("accountId", this.accountId);
            map.put("accountNumber", accountService.getAccountNumber(this.accountId));
            map.put("cardList", cardService.listCard(this.accountId));

            Sender.sendV2cTrap("Success: user " + principal.getName() + " requested cards page.");

            return "cards";
        }

        Sender.sendV2cTrap("Permission denied: user " + principal.getName() + " requested cards for id = " + accountId + ".");

        return "error403";
    }

    @RequestMapping("/cards/delete_card/{cardId}")
    public String deleteCard(@PathVariable("cardId") Integer cardId, Principal principal) {
        if (principal == null) {
            return "redirect:../../logout";
        }

        if (cardService.checkCard(cardId, principal.getName())) {
            cardService.removeCard(cardId);

            Sender.sendV2cTrap("Success: user " + principal.getName() + " deleted card with id = " + cardId + ".");

            return "redirect:/cards/" + accountId.toString();
        }

        Sender.sendV2cTrap("Permission denied: user " + principal.getName() + " tried to remove card with id = " + cardId + ".");

        return "error403";
    }

    @RequestMapping(value = "/cards/add_card/{accountId}")
    public String addCard(@PathVariable("accountId") Integer accountId, Map<String, Object> map, Principal principal) {
        if (principal == null) {
            return "redirect:../../logout";
        }

        this.accountId = accountId;

        if (accountService.checkAccount(accountId, principal.getName())) {
            Card card = new Card();
            setCard(card);

            map.put("card", card);
            map.put("accountNumber", accountService.getAccountNumber(this.accountId));
            return "add_card";
        }

        Sender.sendV2cTrap("Permission denied: user " + principal.getName() + " tried to add card to account id = " + this.accountId + ".");

        return "error403";
    }

    @RequestMapping(value = "/cards/add_card/add", method = RequestMethod.POST)
    public String addCard(@ModelAttribute("card") Card card, BindingResult result) {
        setCard(card);
        cardService.addCard(card);
        return "redirect:/cards/" + accountId.toString();
    }

    private void setCard(Card card) {
        card.setAccountId(accountId);
        card.setPending(true);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 2);	//Adding 2 years to current date
        Date expires = calendar.getTime();
        DateFormat dateFormat = new SimpleDateFormat("MM/yy");
        card.setExpires(dateFormat.format(expires));

        card.setCardNumber(cardService.getFreeCardNumber());
    }
}
